#!/usr/bin/env perl -w
# Time-stamp: <18 Jan 02 15:31:55 giron1>

=head1 NAME

compile_cond.perl - compile condition files for noise_mix

=head1 SYNOPSIS

compile_cond.perl [commands] [options] car type mic env road

=head1 DESCRIPTION

This programs compiles condition files for noise_mix

=head1 SEE ALSO

For more precision about the procedures provided for encoding see also
L<OPT_EXT_HTML/OPT_EXT>

=head1 AUTHOR

 $Id: compile_cond.perl,v 1.1 2007/10/08 09:26:04 schweitz Exp $
 Franck Giron

=head1 LOG

$Log: compile_cond.perl,v $
Revision 1.1  2007/10/08 09:26:04  schweitz
version 2.4, lib vergessen

Revision /main/6  22-Jan-2003.10:04:01  giron1
Changing header to /usr/bin/env perl.

Revision /main/5  19-Apr-2002.16:34:32  giron1
different options added. Determination of package added.

Revision 1.3  2001/03/05 17:05:18  giron1
extended to compile condition directly from environment reference

Revision 1.2  2000/05/12 15:06:19  giron1
changed the order of procedure call to have more flexibility and added pod doc


=cut

#----------------------------------------------------------------------
# Packages 
#----------------------------------------------------------------------
use strict;
use FileHandle;
use OPTIONS;
use OPT_EXT;

#----------------------------------------------------------------------
# MAIN
#----------------------------------------------------------------------

# Define the set of options

my $OptList = new OPTIONS;

$OptList->debug('Do' => 0, 'Usage' => 'debuging mode');
$OptList->list('Do' => 0, 'Usage' => 'list all possible conditions');

$OptList->pkg('string' => 'DCL_Car_Noise',
	      'Usage' => "<name>\t: package name used for condition");
$OptList->cond('string' => 'cond', 'Usage' => "<name>\t: condition basic name");

# voice (input) related options
$OptList->din('string' => '/speech/db', 'Usage' => "<file>\t: speech input directory");
$OptList->ein('string' => '.nist', 'Usage' => "speech input file extension");
$OptList->pin('string' => '$VOICE_POWER_FILE', 'Usage' => "<file>\t: voice power filename");

# noise related options
$OptList->nnoise('string' => 'noise_', 'Usage' => "<file>\t: noise generic name");
$OptList->enoise('string' => '', 'Usage' => "noise extension");
$OptList->snoise('string' => 12000, 'Usage' => "<val>\t: sampling frequency in Hz");

# output related options
$OptList->eout('string' => '.nist', 'Usage' => "output file extension");
$OptList->dout('string' => '/speech/dbwork', 'Usage' => "<name>\t: output directory");

# Command processing

$OptList->GetOpt || die "Check your options\n";

# Check the arguments
my $numargs = @ARGV;

# Eventually define which package is used for the conversion if the argument 
# string contains a string like eX where X is an number
if ($numargs == 1 and $ARGV[$numargs-1] =~ m/e(\d+)/) {
  $OptList->{'pkg'}{'string'} = OPT_EXT::GetPackage($1);
  if (not defined $OptList->{'pkg'}{'string'}) {
    warn "Environment e$1 not yet defined, check your package definitions.\n";
    exit 0;
  }
}
# Defines which package is used for the conversion

my $pkg = new OPT_EXT $OptList->{'pkg'}{'string'};

my @Noise_Cond = @{$pkg->{'data'}->{'Conditions_Keys'}};
my $Noise_Cond_Numb = $#Noise_Cond + 1;

$OptList->{'debug'}{'Do'} and warn "@Noise_Cond\n";

my $usage = ( ($numargs != $Noise_Cond_Numb) && ($numargs != 1) );

if ($usage) {$OptList->Usage("string | @Noise_Cond");}

SWITCH: {
  if ($numargs == 1) {
    $_ = $ARGV[0];
    # the single argument contains a string  like *.e10*
    if (m/\.e.*/) {
      my %Cond = $pkg->Ext2Cond($_);
      $OptList->{'debug'}{'Do'} and warn "\%Cond = ",join(' ',%Cond),"\n";
      my @condlist = $pkg->CompileCond($OptList,\%Cond);
    }
    # the single argument contains a path like estima/st/array/fine/hiway
    else {
      s/^\.?\///;s/\/$//;
      my @envlist = split '/',$_;
      my %Cond = ( 'env' => \@envlist, 'mic' => undef, 'sf' => undef);
      my @condlist = $pkg->CompileCond($OptList,\%Cond);
     }
    last SWITCH;
  }
  # many arguments are given separately
  if ($numargs == $Noise_Cond_Numb) {
    my %Cond = ('env' => \@ARGV, 'mic' => undef, 'sf' => undef);
    my $ext = $pkg->Cond2Ext(\%Cond);
    warn "Extension: $ext\n";
    my @condlist = $pkg->CompileCond($OptList,\%Cond);
    last SWITCH;
  }
}


#$OptList->{'debug'}{'Do'} and warn "@condlist\n";

__END__

=head1 COMMANDS

The following commands are defined:

=over 10

=item -debug	

 debuging mode

=item -list	

 list all possible conditions

=back

=head1 OPTIONS

The following options are defined:

=over 14

=item -cond I<name>	

 condition basic name 
 (default: cond)

=item -din I<file>	

 speech input directory 
 (default: /speech/db)

=item -dout I<name>	

 output directory 
 (default: /speech/dbwork)

=item -ein I<ext>	

 speech input file extension 
 (default: .nist)

=item -enoise I<ext>	

 noise extension 
 (default: )

=item -eout I<ext>	

 output file extension 
 (default: .nist)

=item -nnoise I<file>	

 noise generic name 
 (default: noise_)

=item -pin I<file>	

 voice power filename 
 (default: $VOICE_POWER_FILE)

=item -pkg I<name>	

 package name used for condition 
 (default: DCL_Car_Noise)

=item -snoise I<val>	

 sampling frequency in Hz 
 (default: 12000)

=back
